//
//  LeftView.m
//  Drawer
//
//  Created by Young on 16/8/13.
//  Copyright © 2016年 Smile. All rights reserved.
//

#import "LeftView.h"

#import "UIView+Supplement.h"
#import "DrawerManager.h"

static NSString *const CELL_IDENTIFIER = @"CELL_IDENTIFIER";

#define kBackgroundColor [UIColor colorWithRed:13.f / 255.f green:184.f / 255.f blue:246.f / 255.f alpha:1]

@interface LeftView () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) UIImageView *imageView;

@end

@implementation LeftView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kBackgroundColor;
        self.dataArray = @[@[[UIImage imageNamed:@"sidebar_business"], @"我的商城"],
                           @[[UIImage imageNamed:@"sidebar_purse"], @"我的钱包"],
                           @[[UIImage imageNamed:@"sidebar_decoration"], @"个性装扮"],
                           @[[UIImage imageNamed:@"sidebar_favorit"], @"我的收藏"],
                           @[[UIImage imageNamed:@"sidebar_album"], @"我的相册"],
                           @[[UIImage imageNamed:@"sidebar_file"], @"我的文件"]];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) style:(UITableViewStylePlain)];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self addSubview:self.tableView];
        
        self.tableView.tableHeaderView = self.imageView;
        self.tableView.tableFooterView = [[UIView alloc] init];
    }
    return self;
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CELL_IDENTIFIER];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 44)];
    selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = selectedBackgroundView;
    cell.imageView.image = self.dataArray[indexPath.row][0];
    cell.textLabel.text = self.dataArray[indexPath.row][1];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[DrawerManager sharedDrawerManager] setShowType:DrawerManagerShowTypeLeft];
    
}

#pragma mark - headerView
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, 280)];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.image = [UIImage imageNamed:@"sidebar_bg"];
        _imageView.clipsToBounds = YES;
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header"]];
        imageView.size = CGSizeMake(60, 60);
        imageView.left = 25;
        imageView.top = 64;
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = imageView.width / 2;
        imageView.layer.borderColor = [UIColor whiteColor].CGColor;
        imageView.layer.borderWidth = 1.0f;
        [_imageView addSubview:imageView];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right + 15, imageView.top + 10, _imageView.right - 25, 21)];
        nameLabel.textColor = [UIColor darkGrayColor];
        nameLabel.font = [UIFont systemFontOfSize:21.0f];
        nameLabel.text = @"Hello, world";
        [_imageView addSubview:nameLabel];
    }
    return _imageView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
